package org.zpmis.common.result;

import com.alibaba.fastjson.JSON;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.zpmis.common.utils.StringUtil;
import org.zpmis.common.utils.language.LanguageUtils;

/**
 * Description:
 *  平台通用返回结果
 * @author LENOVO
 */
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class PlatformResult implements Result {
    private static final long serialVersionUID = 874200365941306385L;

    private Integer code;

    private String msg;

    private Object data;

    public PlatformResult setCode(Integer code) {
        this.code = code;
        return this;
    }

    public PlatformResult setMsg(ResultCode resultCode,Object[] params) {
        if(params == null){
            this.msg = LanguageUtils.getMessage(resultCode.name());
        }else{
            resultCode.setMessage(null);
            this.msg = LanguageUtils.getMessage(resultCode.name(),params);
        }
        if(StringUtil.isEmpty(this.msg)){
            this.msg = resultCode.message();
        }
        return this;
    }

    public static PlatformResult success() {
        PlatformResult result = new PlatformResult();
        result.setResultCode(ResultCode.SUCCESS);
        return result;
    }

    public static PlatformResult success(Object data) {
        PlatformResult result = new PlatformResult();
        result.setResultCode(ResultCode.SUCCESS);
        result.setData(data);
        return result;
    }

    public static PlatformResult success(ResultCode resultCode) {
        PlatformResult result = new PlatformResult();
        result.setCode(resultCode.code());
        result.setMsg(resultCode,null);
        return result;
    }

    public static PlatformResult success(ResultCode resultCode, Object data) {
        PlatformResult result = new PlatformResult();
        result.setResultCode(resultCode);
        result.setData(data);
        return result;
    }

    public static PlatformResult failure() {
        PlatformResult result = new PlatformResult();
        result.setResultCode(ResultCode.ERROR);
        return result;
    }
    public static PlatformResult failure(ResultCode resultCode) {
        PlatformResult result = new PlatformResult();
        result.setResultCode(resultCode);
        return result;
    }

    public static PlatformResult failure(ResultCode resultCode, Object data) {
        PlatformResult result = new PlatformResult();
        result.setResultCode(resultCode);
        result.setData(data);
        return result;
    }

    /**
     * 自定义失败类型和提示语
     * @param
     * @return
     */
    public static PlatformResult failure(ResultCode resultCode,String msg){
        PlatformResult result = new PlatformResult();
        result.setCode(resultCode.code());
        result.setMsg(msg);
        return result;
    }

    private void setResultCode(ResultCode code) {
        this.code = code.code();
        this.msg = code.message();
    }


    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
